package com.lantosec.hmrp.modules.uploads.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.core.util.identify.BusinessLicense;
import com.lantosec.hmrp.core.util.identify.BusinessLicenseBean;
import com.lantosec.hmrp.core.util.identify.IdCardBean;
import com.lantosec.hmrp.core.util.identify.Idcard;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import com.lantosec.hmrp.modules.uploads.vo.SysUploadsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 系统上传数据 控制器
 *
 * @author AutoCode
 * @since 2020-02-19
 */
@Api(value = "系统上传数据控制器", tags = "系统上传数据控制器")
@RestController
@RequestMapping("/uploads")
@Log4j2
public class SysUploadsController extends BaseController {
    @Autowired
    private ISysUploadsService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysUploadsVO>> all() {
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<SysUploadsVO>> page(@ApiParam(value = "分页对象", required = true) Page<SysUploads> page, SysUploadsVO condition) {
        page = service.page(page,
                new QueryWrapper<SysUploads>()
//                        .like(StringUtils.isNotEmpty(condition.getUsername()),"username",condition.getUsername())
        );
        return Result.ok(entity2VO(page));
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<List> get(@ApiParam(value = "ID", required = true) @PathVariable(name = "id") String id) {
        List list = service.pictureUrl(id);
        return Result.ok(list);
    }

    @PostMapping("create")
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "系统上传数据VO对象", required = true) @RequestBody @Validated SysUploadsVO vo, BindingResult bindingResult) {
        validationHandler(bindingResult);
        SysUploads entity = vo.convertToEntity();
        service.save(entity);
        return Result.ok(entity.getUploadId());
    }

    @PutMapping("update")
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "系统上传数据VO对象", required = true) @RequestBody SysUploadsVO vo) {
        SysUploads entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name = "ids") String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }

    @PostMapping("saveMoreUploads")
    @ApiOperation("保存多附件")
    public Result<String> saveMoreUploads(
            @RequestBody @Validated @ApiParam(value = "MultipartFile", required = true) MultipartFile[] file) {
        String url = null;
        for (MultipartFile multipartFile : file) {
            url += service.saveUploads(multipartFile);
        }
        return Result.ok(url);
    }

    @PostMapping("saveOnlyUploads")
    @ApiOperation("保存单附件")
    public Result<SysUploadsVO> saveUploads(
            @RequestBody @Validated @ApiParam(value = "MultipartFile", required = true) MultipartFile file) {
        SysUploads sysUploads = service.saveUploads(file);
        return Result.ok(sysUploads.convertToVO());
    }

    @DeleteMapping("deleteFile/{uploadId}")
    @ApiOperation("删除图片")
    public Result<String> deleteFile(@ApiParam(value = "uploadId", required = true) @PathVariable(name = "uploadId") String uploadId) {
        SysUploads sysUploads = service.getById(uploadId);
        //获得图片存放的路径
        String rootPath = InitDataListener.rootPath;
        String savePath = rootPath + sysUploads.getSavePath() + sysUploads.getFileName();
        File file = new File(savePath);
        if (file.exists()) {
            if (!file.delete()) {
                return Result.ok("删除失败");
            }
        }
        service.deleteFileById(uploadId);
        return Result.ok("删除成功");
    }

    @PostMapping("saveIdcardFront")
    @ApiOperation("保存身份证正面附件")
    public Result<List> saveIdcardFront(
            @RequestBody @Validated @ApiParam(value = "MultipartFile", required = true) MultipartFile file) {
        SysUploads sysUploads = service.saveUploads(file);
        //文件存放路径的地址
        String savePath = sysUploads.convertToVO().getLocalUrl();
        String fileName = sysUploads.getFileName();
        //返回给前端可以访问的地址
        sysUploads.setSavePath(InitDataListener.uploadsUrl + sysUploads.getSavePath() + fileName);
        IdCardBean idCardBean = Idcard.idcard("front", savePath + fileName);
        List list = new ArrayList();
        list.add(sysUploads);
        list.add(idCardBean);
        return Result.ok(list);
    }

    @PostMapping("saveIdcardBack")
    @ApiOperation("保存身份证反面附件")
    public Result<List> saveIdcardBack(
            @RequestBody @Validated @ApiParam(value = "MultipartFile", required = true) MultipartFile file) {
        SysUploads sysUploads = service.saveUploads(file);
        //文件存放的路径地址
        String savePath = sysUploads.convertToVO().getLocalUrl();
        String fileName = sysUploads.getFileName();
        //返回给前端可以访问的地址
        sysUploads.setSavePath(InitDataListener.uploadsUrl + sysUploads.getSavePath() + fileName);
        IdCardBean idCardBean = Idcard.idcard("back", savePath + fileName);
        List list = new ArrayList();
        list.add(sysUploads);
        list.add(idCardBean);
        return Result.ok(list);
    }

    @PostMapping("saveQualification")
    @ApiOperation("保存购销资格证附件")
    public Result<List> saveQualification(@RequestBody @Validated @ApiParam(value = "MultipartFile", required = true) MultipartFile file) {
        SysUploads sysUploads = service.saveUploads(file);
        String savePath = sysUploads.getSavePath();
        String fileName = sysUploads.getFileName();
        //调用上传购销资格证接口

        return null;
    }

    @PostMapping("saveBusinessLicense")
    @ApiOperation("保存营业资格证附件")
    public Result<List> saveBusinessLicense(@RequestBody @Validated @ApiParam(value = "MultipartFile", required = true) MultipartFile file) {
        SysUploads sysUploads = service.saveUploads(file);
        //文件存放本地的路径地址
        String savePath = sysUploads.convertToVO().getLocalUrl();
        String fileName = sysUploads.getFileName();
        //返回给前端可以访问的地址
        String path = sysUploads.getSavePath();
        sysUploads.setSavePath(InitDataListener.uploadsUrl + path + fileName);
        //调用上传营业资格证接口
        BusinessLicenseBean businessLicenseBean = BusinessLicense.businessLicense(savePath + fileName);
        List list = new ArrayList();
        list.add(sysUploads);
        list.add(businessLicenseBean);
        return Result.ok(list);
    }
}
